{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir(os.path.split(os.getcwd())[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import gaussian_kde\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.style as style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interact_manual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "style.use('seaborn-poster')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse raw logs and create DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_raw_log(log):\n",
    "    data = []\n",
    "    lines = log.split('\\n')\n",
    "    for line in lines[:-1]:\n",
    "        raw = line.split(': ')\n",
    "        if raw[0].split(', ')[1] == 'LOG':\n",
    "            entries = raw[1].split(', ')\n",
    "            datum = []\n",
    "            for entry in entries:\n",
    "                value = entry.split(':')[1]\n",
    "                try:\n",
    "                    datum.append(float(value))\n",
    "                except ValueError:\n",
    "                    datum.append(value)\n",
    "\n",
    "            data.append(datum)\n",
    "    \n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "experiment_folder = None\n",
    "log_path = os.path.join('experiments', experiment_folder, 'training.log')\n",
    "with open(log_path, 'r') as f:\n",
    "    log = f.read()\n",
    "    \n",
    "data = parse_raw_log(log)\n",
    "\n",
    "columns = ['episode', 'step', 'action', 'kind', 'reward', 'best_mean_reward', 'loss', 'epsilon', 'S', 'c', 'delta', 'n', 'dn', 'cost', 'pnl', 'K', 'T']\n",
    "train_df = pd.DataFrame(data, columns = columns)\n",
    "train_df = train_df.astype({'episode': int, 'step': int, 'action': int, 'dn': int})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df[train_df['episode'] == 1].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualize arbitrary episode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@interact\n",
    "def plot_episode(episode = (1, 30000, 1)):\n",
    "    fig, ax = plt.subplots(figsize = (12, 8))\n",
    "    episode_df = train_df[train_df['episode'] == episode]\n",
    "    ax.plot(episode_df['step'], episode_df['n'], color = 'blue', lw = 1.5, label = 'n')\n",
    "    ax.plot(episode_df['step'], episode_df['delta'] * -100, color = 'orange', lw = 1.5, label = 'delta')\n",
    "    ax.scatter(episode_df[episode_df['kind'] == 'random']['step'], episode_df[episode_df['kind'] == 'random']['n'], color = 'red', label = 'random')#, facecolor = 'None')\n",
    "    ax.scatter(episode_df[episode_df['kind'] == 'policy']['step'], episode_df[episode_df['kind'] == 'policy']['n'], color = 'green', label = 'policy')\n",
    "    ax.legend()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize = (12, 8))\n",
    "groupby_episode = train_df.groupby('episode').mean()\n",
    "rewards = train_df.groupby('episode').sum()['reward'].values[:-1]\n",
    "best_mean_reward = groupby_episode['best_mean_reward'].values[:-1]\n",
    "mean_loss = groupby_episode['loss'].values[:-1]\n",
    "\n",
    "ax.plot(np.arange(1, len(rewards) + 1), np.clip(rewards, -np.inf, None), label = 'reward', color = 'blue', lw = 0.1)\n",
    "ax.plot(np.arange(1, len(best_mean_reward) + 1), np.clip(best_mean_reward, -np.inf, None), label = 'best_mean_reward', color = 'red', lw = 1.5)\n",
    "ax.set_ylabel('reward')\n",
    "ax.set_xlabel('episode')\n",
    "#fig.savefig('reward_vs_episode.png', dpi = 300)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize = (14, 10), nrows = 2, ncols = 1, sharex = True)\n",
    "groupby_episode = train_df.groupby('episode').mean()\n",
    "rewards = train_df.groupby('episode').sum()['reward'].values[:-1]\n",
    "best_mean_reward = groupby_episode['best_mean_reward'].values[:-1]\n",
    "mean_loss = groupby_episode['loss'].values[:-1]\n",
    "\n",
    "ax[0].plot(np.arange(1, len(rewards) + 1), np.clip(rewards, -np.inf, None), label = 'reward', color = 'blue', lw = 0.2)\n",
    "ax[0].plot(np.arange(1, len(best_mean_reward) + 1), np.clip(best_mean_reward, -np.inf, None), label = 'best_mean_reward', color = 'red', lw = 1.5)\n",
    "ax[0].set_ylabel('reward')\n",
    "\n",
    "ax[1].plot(np.arange(1, len(mean_loss) + 1), mean_loss, label = 'loss', color = 'blue', lw = 1)\n",
    "ax[1].set_xlabel('episode')\n",
    "ax[1].set_ylabel('loss')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
